<?php

/**
 * Menashop
 *
 * LICENSE
 *
 * This source file is subject to the license that is bundled
 * with this project in the file LICENSE.txt
 *
 * @category   Zend
 * @package    CartController
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 * @version    1.0.0
 * @since      File available since Release 1.0.0
 */

/**
 * @category   Zend
 * @package    CartController
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 */
class Storefront_CartController extends Zend_Controller_Action
{

    /**
     * The logger
     * @var Zend_Log
     */
    protected $_logger;

    /**
     * Id of language
     * @var integer
     */
    protected $_languageId;

    /**
     * The model for cart
     * @var Storefront_Model_Cart
     */
    protected $_cartModel;

    /**
     * Initialize the controller
     */
    public function init()
    {
        // Get logger
        $this->_logger = Zend_Registry::get('log');

        // Get language from locale
        $locale = Zend_Registry::get('Zend_Locale');
        $languageModel = new Storefront_Model_Language();
        $this->_languageId = $languageModel->findIdByShortCode($locale->getLanguage());
        $this->view->language = $locale->getLanguage();

        // Get category tree
        $categoryModel = new Storefront_Model_Category();
        $this->view->categoryTree = $categoryModel->getTree($this->_languageId);

        // Get cart
        $this->_cartModel = new Storefront_Model_Cart();
        $this->view->cartCount = $this->_cartModel->count();

        // Get current url without language
        $this->view->currentUrl = str_replace('/' . $locale->getLanguage(), '', $this->_request->getRequestUri());

        // Form for search
        $form = new Storefront_Model_FormSearch();
        $form->setAction('/' . $locale->getLanguage() . '/search');
        $this->view->formSearch = $form;

        $this->view->shopName = Zend_Registry::get('config')->shopName;
        $this->view->shopTitle = Zend_Registry::get('config')->shopTitle;
        $this->view->shopCity = Zend_Registry::get('config')->shopCity;
    }

    /**
     * Action for index
     */
    public function indexAction()
    {
        // Check product to add
        $productId = $this->_request->getPost('product');
        if ($productId) {
            $this->_cartModel->addItem($productId, 1);
            $this->view->cartCount++;
        }
        $this->view->referrerUrl = $this->_request->getHeader('REFERER');

        // Get cart items
        $cartItems = $this->_cartModel->getItems();
        $productIds = array_keys($cartItems);
        $productModel = new Storefront_Model_Product();
        $result = $productModel->getByIds($productIds, $this->_languageId);

        // Calculate cart total and get products count for view
        $total = 0;
        $totalCount = 0;
        foreach ($result as $product) {
            $product->count = $cartItems[$product->id];
            $total += $product->count * $product->price;
            $totalCount += $product->count;
        }
        $this->view->products = $result;
        $this->view->total = $total;
        $this->view->totalCount = $totalCount;

        // Get time of execution
        $startTime = Zend_Registry::get('startTime');
        $totalTime = round(microtime(true) - $startTime, 3);
        $this->_logger->debug(__METHOD__." spent $totalTime s");
    }

}

